package com.lry.basic.juc;

import java.util.concurrent.BrokenBarrierException;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.CyclicBarrier;

/**
 * @author:刘仁有
 * @desc:
 * @email:953506233@qq.com
 * @data:2019/6/25
 */
public class TestMyReentrantLock {
    static int i;
    static MyReentrantLock lock = new MyReentrantLock();

    public static void main(String[] args) throws InterruptedException, BrokenBarrierException {
        int concurrent=10000;
        final CyclicBarrier barrier = new CyclicBarrier(concurrent+1);
        final CountDownLatch cdl = new CountDownLatch(concurrent);
        for(int i=0;i<concurrent;i++){
            new Thread(new Runnable() {
                @Override
                public void run() {
                    try {
                        cdl.await();
                        add();//10000个线程同时自增
                        barrier.await();
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
            }).start();
            cdl.countDown();
        }
        barrier.await();
        System.out.println(i);
    }

    public static void add(){
        lock.lock();
        ++i;
        lock.unlock();
    }
}
